"""
简单黑方策略
黑方无人艇生成后笔直朝封锁线行驶

作者：wjttdbx
版本：1.0
"""
import sys
sys.path.insert(0, sys.path[0]+"/../")
import numpy as np
import math
from typing import List
from sh15.core.config import GameConfig
from sh15.core.units import BlackBoat, WhiteBoat
from sh15.core.CoreEnvironment import BoundarySystem
from sh15.agents.black_agent import BlackAgent


class SimpleBlackAgent(BlackAgent):
    """
    简单黑方AI智能体
    
    策略：每艘黑方无人艇生成后都笔直朝突破线（A1A6线）行驶
    """
     # 固定随机种子以确保可重复性
    def __init__(self):
        """初始化简单黑方AI"""
        np.random.seed(GameConfig.seed) 
        super().__init__()
        self.strategy = "straight_rush"
        
    def update_all_black_boats(self, black_boats: List[BlackBoat], white_boats: List[WhiteBoat], 
                              current_time: float, dt: float):
        """
        更新所有黑方无人艇
        
        简单策略：每艘船都直接朝突破线冲刺
        """
        for black_boat in black_boats:
            if not black_boat.active or black_boat.is_frozen(current_time):
                continue
            
            # 计算朝突破线的移动
            target_x, target_y = self._calculate_straight_target(black_boat)
            
            # 执行移动
            self._move_towards_target(black_boat, target_x, target_y, dt)
            
            # 边界约束和突破检查
            BoundarySystem.enforce_boundaries(black_boat)
            BoundarySystem.check_breakthrough(black_boat)
    
    def _calculate_straight_target(self, black_boat: BlackBoat) -> Tuple[float, float]:
        """
        计算直线目标位置
        
        目标：A1A6突破线上与当前Y坐标相同的点
        """
        # A1A6是一条垂直线，X坐标为50000
        target_x = 50000  # 突破线的X坐标 - 修正为正确值
        target_y = black_boat.y  # 保持当前Y坐标，直线前进
        
        return target_x, target_y
    
    def _move_towards_target(self, black_boat: BlackBoat, target_x: float, target_y: float, dt: float):
        """移动到目标位置"""
        dx = target_x - black_boat.x
        dy = target_y - black_boat.y
        distance = math.sqrt(dx*dx + dy*dy)
        
        if distance > 0:
            heading = math.atan2(dy, dx)
            speed = black_boat.max_speed
            black_boat.move_with_kinematics(speed, heading, dt)
        else:
            # 已到达目标，停止移动
            black_boat.move_with_kinematics(0.0, black_boat.heading, dt)
    
    def should_attempt_lock(self, black_boat: BlackBoat, white_boats: List[WhiteBoat]) -> bool:
        """
        简单策略不进行锁定，专注突破
        
        Returns:
            bool: 总是返回False，不尝试锁定
        """
        return True
    
    def reset(self):
        """重置智能体状态"""
        pass 